/*
* dataview 私有方法  流程
* */
const dvFunPrivateFlow = {

  //批量提交流程，版本1，调用 flow_change_list
  flowChangeBatch1(button, checkRows) {
    this.utilVue.loadShow(true)
    let params = []
    let ids = checkRows.map(row => row.id)
    let flowStatusTextFrom = this.getFlowStatusText(button.valueIf)
    let flowStatusTextTo = this.getFlowStatusText(button.valueTo)
    if (!flowStatusTextFrom || !flowStatusTextTo) {
      return
    }
    let busTypeName = '未知类型'
    if (this.$route && this.$route.meta && this.$route.meta.title) {
      busTypeName = this.$route.meta.title
    }

    for (let j = 0; j < ids.length; j += 1) {
      let param = {
        busId: ids[j],
        buttonKey: button.key,
        flowKey: this.propFlowKey,
        busIdName: this.primaryKey,
        busTable: this.baseTable,
        'flowStatusTextFrom': flowStatusTextFrom,
        'flowStatusTextTo': flowStatusTextTo,
        'busTypeName': busTypeName,
      }
      params.push(param)
    }
    let url = this.flowEntry.substring(0, this.flowEntry.lastIndexOf('/')) + '/flow_change_list'
    this.$ajax.post(url, params).then(resp => {
      this.utilVue.loadHide()
      if (resp.success) {
        this.utilMessage.success(resp.msg)
        this.handleQueryClick()
      }
    })
  },

  //批量提交流程，版本2，调用 flow_change_batch
  flowChangeBatch2(button, checkRows) {
    this.utilVue.loadShow(true)

    let ids = checkRows.map(row => row.id)
    let flowStatusTextFrom = this.getFlowStatusText(button.valueIf)
    let flowStatusTextTo = this.getFlowStatusText(button.valueTo)
    if (!flowStatusTextFrom || !flowStatusTextTo) {
      return
    }

    let busTypeName = '未知类型'
    if (this.$route && this.$route.meta && this.$route.meta.title) {
      busTypeName = this.$route.meta.title
    }
    let params = {
      busIdList: ids,
      flowKey: this.propFlowKey,
      buttonKey: button.key,
      busIdName: this.primaryKey,
      busTable: this.baseTable,
      'flowStatusTextFrom': flowStatusTextFrom,
      'flowStatusTextTo': flowStatusTextTo,
      'busTypeName': busTypeName,
    }
    let url = this.flowEntry.substring(0, this.flowEntry.lastIndexOf('/')) + '/flow_change_batch'
    this.$ajax.post(url, params).then(resp => {
      this.utilVue.loadHide()
      if (resp.success) {
        this.utilMessage.success(resp.msg, 3000)
        this.handleQueryClick()
      }
    })
  },
  // 编辑界面流程按钮事件
  handleViewFormFlow(button, data, ignoreBeforeFlow = false) {
    if (this.strings.isBlank(this.flowEntry)) {
      return
    }
    this.ignoreBeforeFlow = ignoreBeforeFlow

    const {beforeFlow, afterFlow} = this.viewFormEvents

    if (!this.ignoreBeforeFlow) {
      if (beforeFlow && !beforeFlow(button.code, button)) {
        return
      }
    }

    this.clickedFlowButton = button
    this.flowCommentForm = {}

    //需要用户确认
    if (button.flagConfirm != undefined && (button.flagConfirm == true || button.flagConfirm == '1' || true == button.flagEnd)) {
      let confirmMsg = this.strings.defaultIfBlank(button.confirmMsg, `确认执行 【${button.title}】么`)
      this.utilMessage.confirm(confirmMsg, () => {
        this.handleViewFormFlowCall(button)
      })
      return
    }
    this.handleViewFormFlowCall(button)
  },
  // 编辑界面流程按钮事件
  handleViewFormFlowCall(button) {
    // 是否需要审批意见
    let {flagComment} = button
    if (flagComment) {
      this.formAuditShow()
    } else {
      this.submitFlow()
    }
  },

  // 提交流程
  submitFlow(flagSaveIgnoreCheck = false) {
    const {beforeFlow, afterFlow, validate} = this.viewFormEvents

    //

    let {comment, attachmentUrl, signBase64} = this.flowCommentForm
    let button = this.clickedFlowButton
    let valueTo = null

    if (!this.ignoreBeforeFlow) {
      if (beforeFlow && !beforeFlow(button.valueTo, button)) {
        return {code: 1003, msg: '流程检查未通过'}
      }
    }


    let flowStatusTextFrom = this.getFlowStatusText(button.valueIf)
    let flowStatusTextTo = this.getFlowStatusText(button.valueTo)
    if (!flowStatusTextFrom || !flowStatusTextTo) {
      return
    }

    let busTypeName = '未知类型'
    if (this.$route && this.$route.meta && this.$route.meta.title) {
      busTypeName = this.$route.meta.title
    }

    let entityWeb = null
    if (true == button.flagSave) {
      if (!flagSaveIgnoreCheck ) {
        const {beforeSave} = this.viewFormEvents
        if (beforeSave && !beforeSave()) {
          this.utilMessage.warning('保存校验未通过，请检查')
          this.utilVue.loadHide()
          return
        }
        this.utilVue.loadHide()
        validate().then((result) => {
          if (result && result == true) {
            this.submitFlow(true)
          } else {
            this.utilMessage.warning('请先填写表单必填项，再提交')
          }
        })
        return
      }
      entityWeb = this.viewFormData
    }
    this.utilVue.loadShow(true)
    let params = {
      flowKey: this.flow.code,
      buttonKey: button.code,
      busId: this.viewFormData[this.primaryKey],
      busIdName: this.primaryKey,
      busTable: this.baseTable,
      entityWeb: entityWeb,
      'flowStatusTextFrom': flowStatusTextFrom,
      'flowStatusTextTo': flowStatusTextTo,
      'busTypeName': busTypeName,
      comment,
      attachmentUrl,
      signBase64,
    }
    //追加拦截器需要使用的参数
    params = this.utils.appendParamForInterceptor(this, 'dataviewflow', params, this.clickedFlowButton)

    // 提交流程
    this.$ajax
      .post(this.flowEntry, params)
      .then((resp) => {
        this.clickedFlowButton = false
        if (resp.success) {
          this.utilMessage.success(resp.msg)
          valueTo = button.valueTo
          this.ocRefreshCurrentRow()
          //放到关闭窗口后刷新数据
          // this.fetchData()
          //返回新的流程值
          if (afterFlow) {
            try {
              afterFlow(valueTo, this, button.code)
            } catch (e) {
              console.error('afterFlow.error', e)
            }
          }
          this.formAuditHide()
          let fieldName = strings.camelCase(this.flow.fieldName)
          this.$set(this.viewFormData, this.flow.fieldName, button.valueTo)
          this.$set(this.viewFormData, fieldName, button.valueTo)
          this.$refs.viewForm.form[this.flow.fieldName] = button.valueTo
          this.$refs.viewForm.form[fieldName] = button.valueTo

          // 表单未禁用，则设置viewFormReadOnly未禁用，不然初始化viewFormReadOnly为true，会显示异常
          if (!this.cpFormReadonly) {
            this.viewFormReadOnly = false
          }
        }

        this.utilVue.loadHide()
      })
      .catch(() => {
        this.utilVue.loadHide()
        this.clickedFlowButton = false
      })
  }

}

export default dvFunPrivateFlow
